package spring.cloud.tasks.executor.listener.sharding;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.executor.listener.election.ElectionNode;
import spring.cloud.tasks.executor.listener.task_execute.ExecutorNode;
import spring.cloud.tasks.executor.node.TaskNodePaths;

/**
 * Saturn分片节点名称的常量类.
 */
@Slf4j
@Setter
@Getter
public final class ShardingNode {

    public static final String LEADER_SHARDING_ROOT = ElectionNode.ROOT + "/sharding";

    public static final String NECESSARY = LEADER_SHARDING_ROOT + "/necessary";

    public static final String PROCESSING = LEADER_SHARDING_ROOT + "/processing";

    private static final String SERVER_SHARDING = ExecutorNode.ROOT + "/%s/sharding";

    private final String jobName;

    public ShardingNode(String jobName) {
        this.jobName = jobName;
    }

    public static String getShardingNode(final String executorName) {
        return String.format(SERVER_SHARDING, executorName);
    }

    /**
     * 判断是否为需要重新做sharding的Path
     *
     * @param path 节点路径
     * @return 判断是否为需要重新做sharding的Path
     */
    public boolean isShardingNecessaryPath(final String path) {
        return TaskNodePaths.getTaskNodePath(jobName, NECESSARY).equals(path);
    }
}
